﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Media;
using CPETwit.WPFClient.Business.Service;
using CPETwit.WPFClient.Business.TwitterServiceReference;
using CPETwit.WPFClient.Command;
using CPETwit.WPFClient.Common;
using CPETwit.WPFClient.Helper;
using System.Linq;

namespace CPETwit.WPFClient.ViewModel
{
    public class TweetMenuViewModel : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        private readonly WCFServiceCaller _serviceCaller;

        private ICommand _createMessageCommand;
        private string _messageContent;
        private User _currentUser;
        private bool _tweetOnTwitter;

        public User CurrentUser
        {
            get { return _currentUser; }
            set
            {
                _currentUser = value;
                OnPropertyChanged("CurrentUser");
            }
        }
        public string MessageContent
        {
            get { return _messageContent; }
            set
            {
                _messageContent = value;
                OnPropertyChanged("MessageContent");
            }
        }
        public ICommand CreateMessageCommand
        {
            get
            {
                return _createMessageCommand ??
                    new RelayCommand(param => CreateMessage(),
                                     param => CanCreateMessage());
            }
        }

        public bool TweetOnTwitter
        {
            get { return _tweetOnTwitter; }
            set
            {
                _tweetOnTwitter = value;
                OnPropertyChanged("TweetOnTwitter");
            }
        }

        public TweetMenuViewModel()
        {
            _serviceCaller = CommonData.GetServiceCaller();

            if (App.IsInDesignMode())
            {
                UpdateViewModel(new User
                {
                    name = "Pierre Gillon",
                    avatar = null
                });
            }
            else
            {
                if (CommonData.LOGGED_USER != null)
                    UpdateViewModel(CommonData.LOGGED_USER);
            }
        }

        private void CreateMessage()
        {
            _serviceCaller.CreateMessage(MessageContent, CommonData.LOGGED_USER, _tweetOnTwitter);
            MessageContent = string.Empty;
            ServiceManager.RefreshUser(_currentUser);
            DialogManager.UpdateTweetMenus();
            DialogManager.UpdateMainTimeLine();
        }
        private bool CanCreateMessage()
        {
            return string.IsNullOrEmpty(MessageContent) == false;
        }

        public void UpdateViewModel(User user)
        {
            if (user == null) throw new ArgumentNullException("user");
            CurrentUser = user;
        }
    }
}
